﻿using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour 
{
	
	private float mouseX;
	private float mouseY;
	private float camSize=20;
	private float scrollSpeed = 30.0f;
	private float scrollDistance = 10.0f;
	private float scrollAmount;
	private Vector3 localVec;
	private float xMin,xMax,zMin,zMax;
	public AudioClip[] music = new AudioClip[3];
	
	// Use this for initialization
	void Start () 
	{
		localVec = transform.localPosition;
		Vector3 topLeft = new Vector3 (100,0, 100) + localVec;
		Vector3 botRight = new Vector3 (-100,0, -100) + localVec;
		camSize = gameObject.GetComponent<Camera>().orthographicSize;
		xMin = botRight.x+camSize/2;
		xMax = topLeft.x-camSize/2;
		zMin = botRight.z+camSize/2;
		zMax = topLeft.z - camSize / 2;
		int track = Random.Range (0,2);
		this.audio.clip = music[track];
		this.audio.Play ();
	}
	
	// Update is called once per frame
	void LateUpdate () 
	{	
		//Determine if mouse is being used to scroll across the map, and if so, scroll
		mouseX = Input.mousePosition.x;
		mouseY = Input.mousePosition.y;
		scrollAmount = scrollSpeed * Time.deltaTime;
		
		if(((mouseX < scrollDistance) || Input.GetKey (KeyCode.LeftArrow)) && transform.position.x > xMin)
		{
			transform.Translate(-scrollAmount, 0.0f, 0.0f, Space.World);
		}
		
		if(((mouseX > Screen.width - scrollDistance) || Input.GetKey (KeyCode.RightArrow)) && transform.position.x < xMax)
		{
			transform.Translate (scrollAmount, 0.0f, 0.0f, Space.World);
		}
		
		if(((mouseY < scrollDistance) || Input.GetKey (KeyCode.DownArrow)) && transform.position.z > zMin)
		{
			transform.Translate(0.0f, 0.0f, -scrollAmount, Space.World);
		}
		
		if(((mouseY > Screen.height - scrollDistance) || Input.GetKey (KeyCode.UpArrow)) && transform.position.z < zMax)
		{
			transform.Translate (0.0f, 0.0f, scrollAmount, Space.World);
		}
	}

	public void moveToLocation(float x, float z)
	{
		transform.Translate(x + 10 - transform.position.x, 0.0f, z + 10 - transform.position.z, Space.World);
	}
}
